import { defHttp } from '/@/utils/http/axios';
import { useMessage } from '/@/hooks/web/useMessage';

const { createConfirm } = useMessage();

enum Api {
  list = '/nurseuser_profile/nurseuserProfile/list',
  listAll = '/nurseuser_profile/nurseuserProfile/listAll',
  save = '/nurseuser_profile/nurseuserProfile/add',
  edit = '/nurseuser_profile/nurseuserProfile/edit',
  deleteOne = '/nurseuser_profile/nurseuserProfile/delete',
  deleteBatch = '/nurseuser_profile/nurseuserProfile/deleteBatch',
  importExcel = '/nurseuser_profile/nurseuserProfile/importExcel',
  exportXls = '/nurseuser_profile/nurseuserProfile/exportXls',
  addToNurses = '/nurse_nurses/nurseNurses/add', // 新增到nurse_nurses表的接口
  updateUserInfoAfterProfile = '/nurse_hospital/nurseHospital/updateUserInfoAfterProfile', // 审核通过后更新用户信息接口
  getDepartName = '/sys/sysDepart/getDepartName',
  queryAllTreeListByPid = '/sys/sysDepart/queryAllTreeListByPid',
  sendSubscribeMsg = '/WeChat/sendSubscribeMsg', // 发送微信订阅消息接口
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;
/**
 * 列表接口
 * @param params
 */
export const list = (params) => defHttp.get({ url: Api.list, params });

/**
 * 根据type参数动态选择列表接口
 * @param params
 * @param hasType 是否有type参数
 */
export const listByType = (params, hasType: boolean) => {
  const url = hasType ? Api.list : Api.listAll;
  return defHttp.get({ url, params });
};

/**
 * 删除单个
 */
export const deleteOne = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};
/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};
/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params });
};

/**
 * 审核状态（使用编辑接口）
 * @param params
 */
export const auditStatus = (params) => {
  return defHttp.post({ url: Api.edit, params });
};

/**
 * 更新flowId
 * @param params 包含id和flowId的参数对象
 */
export const updateFlowId = (params) => {
  return defHttp.post({ url: Api.edit, params });
};

/**
 * 审核通过后向nurse_nurses表插入数据
 * @param params
 */
export const addToNursesTable = (params) => {
  return defHttp.post({ url: Api.addToNurses, params });
};

/**
 * 审核通过后更新用户信息
 * @param params 包含idcard、openid、hospital的参数对象
 */
export const updateUserInfoAfterProfile = (params) => {
  return defHttp.get({ url: Api.updateUserInfoAfterProfile, params });
};

/**
 * 根据机构编码获取部门信息
 * @param params
 */
export const getDepartName = (params) => {
  return defHttp.get({ url: Api.getDepartName, params });
};

/**
 * 查询部门树形结构
 * @param params
 */
export const queryAllTreeListByPid = (params) => {
  return defHttp.get({ url: Api.queryAllTreeListByPid, params });
};

/**
 * 发送微信订阅消息
 * @param params 包含 template、appid、appsecret、openid、page、phone 的参数对象
 */
export const sendSubscribeMsg = (params) => {
  // 后端使用 @RequestParam，需要将参数转换为查询字符串
  const urlParams = new URLSearchParams(params).toString();
  return defHttp.post({
    url: `${Api.sendSubscribeMsg}?${urlParams}`,
  });
};
